package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.entity.*;
import com.project.mapper.IUserMapper;
import com.project.mapper.IUserOrderInfoProductMapper;
import com.project.mapper.IUserOrderMapper;
import com.project.service.IUserOrderService;
import com.project.util.ConvertBeanUtil;
import com.project.vo.UserOrderVo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 购物结算
 */
@Service
@Transactional
public class UserOrderServiceImpl implements IUserOrderService {

    @Autowired
    private IUserOrderMapper userOrderMapper;

    @Autowired
    private IUserMapper userMapper;

    @Autowired
    private IUserOrderInfoProductMapper infoProductMapper;



    @Override
    public Page<UserOrderVo> findByItem(int pageNO, int status, int pageSize) {
        QueryWrapper<UserOrderEntity> qw = new QueryWrapper<>();
        qw.eq("o_status", status);
        Page<UserOrderEntity> userOrderEntityPage = userOrderMapper.selectPage(new Page<>(pageNO, pageSize), qw);
        List<UserOrderEntity> records = userOrderEntityPage.getRecords();
        for (UserOrderEntity u: records) {
            QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("u_num", u.getUserRef());
            UserEntity userEntity = userMapper.selectOne(queryWrapper);
            u.setUserEntity(userEntity);
        }

        List<UserOrderVo> userOrderVoList = ConvertBeanUtil.copyList(userOrderEntityPage.getRecords(), UserOrderVo.class
                ,(convertBefore, convertAfter) -> {
                    convertAfter.setUserRef(String.valueOf(convertBefore.getUserRef()));
                });

        Page<UserOrderVo> copy = ConvertBeanUtil.copy(userOrderEntityPage, Page.class,
                (convertBefore, convertAfter) -> {
                    convertAfter.setRecords(userOrderVoList);
                });

        return copy;
    }

    @Override
    public List<UserOrderInfoProductEntity> findById(String customerId) {
        QueryWrapper<UserOrderInfoProductEntity> qw = new QueryWrapper<>();
        qw.eq("fk_orderRef", customerId);
        return infoProductMapper.selectList(qw);
    }







}
